<!--
 * @Author: daifen
 * @Date: 2020-06-30 15:29:14
 * @LastEditors: daifen
--> 
<template>
  <div>
    <div style="width:100%;height:100%;" :id="echarts" class="echarts" ref="echarts"></div>
  </div>
</template>

<script>
// 引入echarts
import echarts from "echarts";
export default {
  name: "commonBarChart",
  props: {
    // chartData中的值
    chartData: {
      type: Array,
      default: () => []
    },
    //标题文字
    title:{
        type:String,
        default:''
    }
  },
  data() {
    return {
        xData:[],
        seriesData:[]
    };
  },
  methods: {
    drawChart() {
      const self = this;
      self.handleChartData();
      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(document.getElementById(this.echarts));
      // 绘制图表
      myChart.setOption({
        title: {
          text:self.title
        },
        tooltip: {},
        xAxis: {
          data: self.xData
        },
        yAxis: {},
        series: [
          {
            name: "",
            type: "bar",
            data: self.seriesData
          }
        ]
      });
    },
    //处理传过来的json中的值,变成两个数组
    handleChartData(){
        const self=this;
        this.chartData.forEach(val=>{
            self.xData.push(val.name);
            self.seriesData.push(val.value);
        })
    }
  },
  computed: {
    echarts() {
      return "echarts" + Math.random() * 100000;
    }
  },
  mounted: function() {
    const vm = this;
    vm.$nextTick(() => {
      vm.drawChart();
    });
  },
  created: () => {}
};
</script>

<style scoped>
</style>